# == Schema Information
#
# Table name: products
#
#  id                 :integer(4)      not null, primary key
#  name               :string(255)
#  productcategory_id :integer(4)
#  serial             :string(255)
#  color              :string(255)
#  price              :integer(4)
#  introduction       :text
#  image_file_name    :string(255)
#  image_content_type :string(255)
#  image_file_size    :integer(4)
#  image_updated_at   :datetime
#  is_public          :boolean(1)
#  created_at         :datetime
#  updated_at         :datetime
#

class Product < ActiveRecord::Base


  has_many :product_images
  belongs_to :productcategory
  validates_presence_of :name
  validates_presence_of :introduction
#  validates_presence_of :price
#  validates_length_of :introduction, :within => 1..5000 , :allow_blank=> true
#  validates_length_of :name, :within => 1..60 , :allow_blank=> true
#  validates_length_of :serial, :within => 1..60 , :allow_blank=> true
#  validates_numericality_of :price , :less_than=>5000000.00

    has_attached_file :image,
    :url => '/system/pictures/:id/:style.:extension',
    :path => ':rails_root/public/system/pictures/:id/:style.:extension',
    :styles => {
    :thumb => '162x122>' ,
    :product => "550x413>"

  } ,   :default_style => :product

  def first_list_image
   if  self.try(:product_images).try(:first).try(:image)
     self.try(:product_images).try(:first).try(:image).url(:list).to_s
   else
     ''
   end
  end
    

end
